package com.czl.module_storehouse.mvp.presenter;

import com.czl.base.util.ToastHelper;
import com.czl.module_base.bean.CompanyBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.BasePresenter;
import com.czl.module_base.mvp.view.IView;
import com.czl.module_base.observer.AbsHandleSubscriber;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.bean.AddSupplierBean;
import com.czl.module_storehouse.bean.CityBean;
import com.czl.module_storehouse.bean.DistrictBean;
import com.czl.module_storehouse.bean.ProvinceBean;
import com.czl.module_storehouse.mvp.model.SupplierModel;

/**
 * @author by xubin,
 * @date on 2022/2/14.
 * PS: Not easy to write code, please indicate.
 */
public class SupplierPresenter extends BasePresenter<SupplierModel, IView> {
    public static final String TAG_SUPPLIER_ADD = "supplier_add";
    public static final String TAG_SUPPLIER_UPDATE = "supplier_update";
    public static final String TAG_SUPPLIER_LIST = "supplier_list";
    public static final String TAG_SUPPLIER_DELETE = "supplier_delete";
    public static final String TAG_PROVINCE = "supplier_province";
    public static final String TAG_CITY = "supplier_city";
    public static final String TAG_DISTRICT = "supplier_district";
    public static final String TAG_COMPANY_INFO = "supplier_company_info";

    public void selectCrmCompanyByTag(boolean refresh, boolean showDialog, String companyName) {
        resetPageNo(refresh);
        doSubscribe(mModel.selectCrmCompanyByTag(Constants.CompanyTag.TAG_SUPPLIER, getPageNo(), companyName), new AbsHandleSubscriber<HttpResponse<ListBean<CompanyBean>>>() {
            @Override
            public void onSuccess(HttpResponse<ListBean<CompanyBean>> httpResponse) {
                httpResponse.setRequestTag(TAG_SUPPLIER_LIST);
                mView.showData(httpResponse.getData());
                nextPageNo();
            }
        }, true, showDialog, null);
    }

    public void addCompany(AddSupplierBean addSupplierBean) {
        doSubscribe2(mModel.addCompany(addSupplierBean), new AbsHandleSubscriber<HttpResponse<CompanyBean>>() {
            @Override
            public void onSuccess(HttpResponse<CompanyBean> httpResponse) {
                String companyId = httpResponse.getData().getCompanyId();
                addCrmCompany(companyId);
            }
        });
    }

    public void addCrmCompany(String crmCompanyId) {
        doSubscribe2(mModel.addCrmCompany(crmCompanyId), new AbsHandleSubscriber<HttpResponse<CompanyBean>>() {
            @Override
            public void onSuccess(HttpResponse<CompanyBean> httpResponse) {
                httpResponse.setRequestTag(TAG_SUPPLIER_ADD);
                mView.showHttpResponse(httpResponse);
            }
        });
    }

    public void deleteCrmCompany(String crmCompanyId) {
        doSubscribe2(mModel.deleteCrmCompany(crmCompanyId), new AbsHandleSubscriber<HttpResponse<CompanyBean>>() {
            @Override
            public void onSuccess(HttpResponse<CompanyBean> httpResponse) {
                ToastHelper.INSTANCE.showNormalToast("删除成功");
                httpResponse.setRequestTag(TAG_SUPPLIER_DELETE);
                mView.showHttpResponse(httpResponse);
            }
        });
    }

    public void updateCompany(AddSupplierBean addSupplierBean, CompanyBean companyBean) {
        doSubscribe2(mModel.updateCrmCompany(addSupplierBean, companyBean), new AbsHandleSubscriber<HttpResponse<CompanyBean>>() {
            @Override
            public void onSuccess(HttpResponse<CompanyBean> httpResponse) {
                httpResponse.setRequestTag(TAG_SUPPLIER_UPDATE);
                mView.showHttpResponse(httpResponse);
            }
        });
    }

    public void selectProvince() {
        doSubscribe2(mModel.selectProvince(), new AbsHandleSubscriber<HttpResponse<ListBean<ProvinceBean>>>() {
            @Override
            public void onSuccess(HttpResponse<ListBean<ProvinceBean>> httpResponse) {
                httpResponse.setRequestTag(TAG_PROVINCE);
                mView.showHttpResponse(httpResponse);
            }
        }, false);
    }

    public void selectCity(String provinceCode) {
        doSubscribe2(mModel.selectCity(provinceCode), new AbsHandleSubscriber<HttpResponse<ListBean<CityBean>>>() {
            @Override
            public void onSuccess(HttpResponse<ListBean<CityBean>> httpResponse) {
                httpResponse.setRequestTag(TAG_CITY);
                mView.showHttpResponse(httpResponse);
            }
        }, false);
    }

    public void selectDistrict(String cityCode) {
        doSubscribe2(mModel.selectDistrict(cityCode), new AbsHandleSubscriber<HttpResponse<ListBean<DistrictBean>>>() {
            @Override
            public void onSuccess(HttpResponse<ListBean<DistrictBean>> httpResponse) {
                httpResponse.setRequestTag(TAG_DISTRICT);
                mView.showHttpResponse(httpResponse);
            }
        }, false);
    }

    public void selectByCompanyId(String companyId) {
        doSubscribe2(mModel.selectByCompanyId(companyId), new AbsHandleSubscriber<HttpResponse<CompanyBean>>() {
            @Override
            public void onSuccess(HttpResponse<CompanyBean> httpResponse) {
                httpResponse.setRequestTag(TAG_COMPANY_INFO);
                mView.showHttpResponse(httpResponse);
            }
        });
    }
}
